package fu.capstone.hanoicityguide.presentation.fragment.itinerary;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.RelativeLayout;

import com.mapquest.android.maps.DefaultItemizedOverlay;
import com.mapquest.android.maps.GeoPoint;
import com.mapquest.android.maps.MapView;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.itinerary.ItineraryBO;
import fu.capstone.hanoicityguide.business.bo.itinerary.ItineraryBOImp;
import fu.capstone.hanoicityguide.business.model.Place;
import fu.capstone.hanoicityguide.common.onlinemapultility.OnlineMapUtil;
import fu.capstone.hanoicityguide.common.utility.InternetSetting;
import fu.capstone.hanoicityguide.common.utility.LocationSetting;

public class ItineraryOnlineMapFragment extends Fragment{

	private MapView map;
	private WebView instruction;
	private RelativeLayout maplayout;
	private RelativeLayout webview;
	private OnlineMapUtil util;
	private LocationSetting settingGPS;
	private InternetSetting settingInternet;
	private Context context;
	private boolean isGPSEnabled;
	private boolean isInternetEnabled;
	private ItineraryBO itineraryBO;
	private GeoPoint place_geo;
	private ArrayList<String> arr_itinerary_geo;
	private Menu menu = null;
	private ArrayList<Place> arrPlace;
	private DefaultItemizedOverlay overlay;
	
	public ItineraryOnlineMapFragment() {}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View itineraryOnlineMapFragment = inflater.inflate(R.layout.itinerary_online_map_fragment, container, false);
		setHasOptionsMenu(true);
		util = new OnlineMapUtil();
		context = container.getContext();
		
		Drawable icon =  context.getResources().getDrawable(R.drawable.location_marker);
		overlay = new DefaultItemizedOverlay(icon);
		map = (MapView) itineraryOnlineMapFragment.findViewById(R.id.mapview);
		instruction = (WebView) itineraryOnlineMapFragment.findViewById(R.id.webview);
		maplayout = (RelativeLayout) itineraryOnlineMapFragment
				.findViewById(R.id.maplayout);
		webview = (RelativeLayout) itineraryOnlineMapFragment
				.findViewById(R.id.itineraryLayout);
		
		settingGPS = new LocationSetting(context);
		settingInternet = new InternetSetting(context);
		arr_itinerary_geo = new ArrayList<String>();
		arrPlace = new ArrayList<Place>();
		
		isGPSEnabled = settingGPS.getGPSSetting(context);
		isInternetEnabled = settingInternet.getConnectivityStatus(context);
		
		if (!isGPSEnabled) {
			settingGPS.showSettingsAlert();
		} else if (!isInternetEnabled) {
			settingInternet.showSettingsAlert();
		} else {
//			util = new OnlineMapUtil();
			util.setupmap(map);
			Bundle bundle = this.getArguments();
			int itineraryID = bundle.getInt("ItineraryID", 0);
			if (itineraryID != 0) {

				itineraryBO = new ItineraryBOImp(container.getContext());
				arrPlace = itineraryBO
						.browseItinerary(itineraryID);
				for (int i = 0; i < arrPlace.size(); i++ ){
					place_geo = util.convertGeocoordinatetoGeopoint(arrPlace.get(i)
						.getGeo());
					util.createMarker(container.getContext(), map, overlay, place_geo, i+1 + ". " +
							arrPlace.get(i).getName(), arrPlace.get(i).getCategory() + "-" +arrPlace.get(i).getType());
				}
				
				for (int i = 0; i < arrPlace.size(); i++) {
					arr_itinerary_geo.add(String.valueOf
							(util.convertGeocoordinatetoGeopoint(arrPlace.get(i).getGeo())).substring(9));
				}
				
//				map.getController().setCenter(util.convertGeocoordinatetoGeopoint(arrPlace.get(0).getGeo()));
//				util.getcurrentLocation(context, map);
//				util.createRoute(context, map, instruction, arr_itinerary_geo);
//				map.getController().setCenter(attraction_geo);
			}
		}
		
		
		return itineraryOnlineMapFragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.map_place_page_menu, menu);
		setMenu(menu);
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_mylocation:
			util.getcurrentLocation(context, map);
			break;
		case R.id.action_routing:
//			util.createRoute(context, map, instruction,
//					String.valueOf(attraction_geo).substring(9));
//			getMenu().getItem(2).setVisible(true);
			util.createRoute(context, map, instruction, arr_itinerary_geo);
			getMenu().getItem(2).setVisible(true);
			break;
		case R.id.action_instruction:
			maplayout.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
			getMenu().getItem(3).setVisible(true);
			getMenu().getItem(0).setVisible(false);
			getMenu().getItem(1).setVisible(false);
			getMenu().getItem(2).setVisible(false);
			break;
		case R.id.action_showmap:
			maplayout.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
			getMenu().getItem(3).setVisible(false);
			getMenu().getItem(0).setVisible(true);
			getMenu().getItem(1).setVisible(true);
			getMenu().getItem(2).setVisible(true);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.getItem(2).setVisible(false);
		menu.getItem(3).setVisible(false);
		super.onPrepareOptionsMenu(menu);
	}
	
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	
	public Menu getMenu() {
		return this.menu;
	}
}
